;;; Temporary; will be removed when `action's are implemented.
(defmethod mind-swap ((guy1 meatbag) (guy2 meatbag))
  (rotatef (ai guy1) (ai guy2)))
(defmethod mind-control ((caster meatbag) (target meatbag))
  (setf (ai target) (ai caster)))
(defmethod mind-control ((caster meatbag) (target vector))
  (dbind (x y) (rescale target)
    (mind-control caster (acquire-target (cell-at-coord (map-of caster) x y)
					 nil))))

(defparameter *controls* :dummy)	;otherwise it doesn't compile


;;; Temporary; will be removed when `action's are implemented.
;;; Orange you glad I didn't say banana? >.>
(defparameter spawn 
  (make-action
   (lambda (meatbag &rest stuff)
     (declare (ignore stuff))
     (let ((color sdl:*black*))
       (push
	(lambda ()
	  (register
	   (destructuring-bind (x y) (rescale (sdl:mouse-position))
	     (make-meatbag x y #'monster-ai
			   :controls *controls*
			   :color color
			   :ap 20 :pic "K" :name (format nil "~a-minion"
							 (name meatbag))))
	   (map-of meatbag)))
	(queue (ai meatbag)))))
   10))

(defparameter debug
  (make-action
   (lambda (meatbag &rest stuff)
     (declare (ignore meatbag stuff))
     (break))))

(defparameter control 
  (make-action
   (lambda (caster &rest stuff)
     (declare (ignore stuff))
     (mind-control caster (sdl:mouse-position)))))


;;; The ugliness of this definition makes me think `make-controls' is not a 
;;; very good abstraction, but I'm at a loss for how to fix it.
(defparameter *controls* 
  (make-controls `((:sdl-key-w . ,(move-function 0 -1))
		   (:sdl-key-s . ,(move-function 0  1))
		   (:sdl-key-a . ,(move-function -1 0))
		   (:sdl-key-d . ,(move-function 1  0))
		   (:sdl-key-q . ,spawn)
		   (:sdl-key-e . ,control)
		   (:sdl-key-escape . ,debug))
		 ;; (make-action
		 ;;  (lambda (meatbag coord buttons)
		 ;;    (when buttons 
		 ;;      (destructuring-bind (x y) coord
		 ;; 	(let ((target 
		 ;; 	       (acquire-target (cell-at-coord (map-of meatbag)
		 ;; 					      x y)
		 ;; 			       nil)))
		 ;; 	  (if target (mind-swap meatbag target)
		 ;; 	      (move meatbag x y)))))))
		 (make-action
		  (lambda (x y button meatbag)
		    (when button
		      (move meatbag x y))))))